package com.ktg.mes.pda.controller;

import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktg.common.core.domain.APIResult;
import com.ktg.mes.pda.domain.PdaLabelType;
import com.ktg.mes.pda.service.IPdaLabelTypeService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktg.common.annotation.Log;
import com.ktg.common.core.controller.BaseController;
import com.ktg.common.enums.BusinessType;
import com.ktg.common.core.domain.Result;
import com.ktg.mes.pda.domain.PdaLabelTypeBom;
import com.ktg.mes.pda.service.IPdaLabelTypeBomService;
import com.ktg.common.utils.poi.ExcelUtil;

/**
 * PDA采集标签内容Controller
 *
 * @author byteco
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/pda/pdaLabelTypeBom")
public class PdaLabelTypeBomController extends BaseController
{
    @Autowired
    private IPdaLabelTypeBomService pdaLabelTypeBomService;

    @Autowired
    private IPdaLabelTypeService pdaLabelTypeService;

    /**
     * 查询PDA采集标签内容列表
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:list')")
    @GetMapping("/list")
    public Result<IPage<PdaLabelTypeBom>> list(PdaLabelTypeBom pdaLabelTypeBom,
                                            @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                            @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                            HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("查询PDA采集标签内容列表",req,null);
        try {
            Page<PdaLabelTypeBom> page = new Page<PdaLabelTypeBom>(pageNo, pageSize);
            IPage<PdaLabelTypeBom> pageList = pdaLabelTypeBomService.queryPageList(pdaLabelTypeBom,page);
            return apiLog.ok(pageList);
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 导出PDA采集标签内容列表
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:export')")
    @Log(title = "PDA采集标签内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PdaLabelTypeBom pdaLabelTypeBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("导出PDA采集标签内容列表",req,pdaLabelTypeBom);
        try {
            List<PdaLabelTypeBom> list = pdaLabelTypeBomService.queryList(pdaLabelTypeBom);
            ExcelUtil<PdaLabelTypeBom> util = new ExcelUtil<PdaLabelTypeBom>(PdaLabelTypeBom.class);
            util.exportExcel(response, list, "PDA采集标签内容数据");
            apiLog.ok(null);
        }catch (Exception e){
            apiLog.error(e);
        }
    }

    /**
     * 获取PDA采集标签内容详细信息
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:query')")
    @GetMapping(value = "/{recordId}")
    public Result<PdaLabelTypeBom> getInfo(@PathVariable("recordId") Long recordId,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("获取PDA采集标签内容详细信息",req,null);
        try {
            return apiLog.ok(pdaLabelTypeBomService.getInfo(recordId));
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 新增PDA采集标签内容
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:add')")
    @Log(title = "PDA采集标签内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public Result<String> add(@RequestBody PdaLabelTypeBom pdaLabelTypeBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增PDA采集标签内容",req,pdaLabelTypeBom);
        try {
            pdaLabelTypeBomService.insert(pdaLabelTypeBom);
            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


    /**
     * 新增PDA采集标签内容
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:add')")
    @Log(title = "PDA采集标签内容", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    public Result<String> addList(@RequestBody List<PdaLabelTypeBom> pdaLabelTypeBomList,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("新增PDA采集标签内容",req,pdaLabelTypeBomList);
        try {
            for (int i = 0;i < pdaLabelTypeBomList.size();i++){
                PdaLabelTypeBom pdaLabelTypeBom = pdaLabelTypeBomList.get(i);

                PdaLabelTypeBom bom = new PdaLabelTypeBom();
                bom.setLabelTypeId(pdaLabelTypeBom.getLabelTypeId());
                bom.setLabelItemId(pdaLabelTypeBom.getLabelItemId());
                QueryWrapper<PdaLabelTypeBom> pdaLabelTypeBomQueryWrapper = new QueryWrapper<>(bom);
                Long a = pdaLabelTypeBomService.count(pdaLabelTypeBomQueryWrapper);

                if ( a == null || a < 1){
                    pdaLabelTypeBomService.insert(pdaLabelTypeBom);
                }
            }
            if (pdaLabelTypeBomList != null && pdaLabelTypeBomList.size() > 0){
                PdaLabelTypeBom bom =  pdaLabelTypeBomService.findTypeBomGroup(pdaLabelTypeBomList.get(0).getLabelTypeId());
                PdaLabelType type = pdaLabelTypeService.getById(bom.getLabelTypeId());
                type.setLabelRule(bom.getName());
                pdaLabelTypeService.updateById(type);
            }

            return apiLog.ok("添加成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }


    /**
     * 修改PDA采集标签内容
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:edit')")
    @Log(title = "PDA采集标签内容", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public Result<String> edit(@RequestBody PdaLabelTypeBom pdaLabelTypeBom,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("修改PDA采集标签内容",req,pdaLabelTypeBom);
        try {
            pdaLabelTypeBomService.update(pdaLabelTypeBom);
            return apiLog.ok("编辑成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }
    }

    /**
     * 删除PDA采集标签内容
     */
    @PreAuthorize("@ss.hasPermi('pda:pdaLabelTypeBom:remove')")
    @Log(title = "PDA采集标签内容", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public Result<String> remove(@PathVariable Long[] recordIds,HttpServletRequest req)
    {
        APIResult apiLog = new APIResult("删除PDA采集标签内容",req,null);
        try {
            PdaLabelTypeBom old = null;
            if (recordIds != null && recordIds.length > 0){
                old = pdaLabelTypeBomService.getById(recordIds[0]);
            }
            pdaLabelTypeBomService.delete(recordIds);
            if (old != null){
                PdaLabelTypeBom bom =  pdaLabelTypeBomService.findTypeBomGroup(old.getLabelTypeId());
                PdaLabelType type = pdaLabelTypeService.getById(bom.getLabelTypeId());
                type.setLabelRule(bom.getName());
                pdaLabelTypeService.updateById(type);
            }
            return apiLog.ok("删除成功！");
        }catch (Exception e){
            return apiLog.error(e);
        }

    }
}
